/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-10-20
*/
package com.rzico.business.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.business.entity.WfajInfo;
import com.rzico.business.service.WfajInfoService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.util.CodeGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 违法案件控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-10-20
 */
@Api(description = "违法案件接口")
@RestController
@RequestMapping("/admin/wfajInfo")
public class WfajInfoController extends BaseController {

    @Autowired
    private WfajInfoService wfajInfoService;

    /**
     * 分页查询违法案件
     *
     * @return
    */
    @ApiOperation("分页查询违法案件")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "hphm", value = "车牌号码", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "clls", value = "车辆类型", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "sjxm", value = "司机姓名", dataType = "String", paramType = "query")

    })
    public CommResult<WfajInfo> list(Date startDate, Date endDate,String hphm,String clls,String sjxm, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (startDate!=null){
           params.put("startDate", startDate);
        }
        if (endDate!=null){
           params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }

        if (hphm!=null){
            params.put("hphm", hphm);
        }

        if (clls!=null){
            params.put("clls", clls);
        }

        if (sjxm!=null){
            params.put("sjxm", sjxm);
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<WfajInfo> list = wfajInfoService.selectList(params);
        PageResult<WfajInfo> pageResult = new PageResult<WfajInfo>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 查询单条违法案件
     *
     * @return
    */
    @ApiOperation("查询单条违法案件")
    @GetMapping("/find/{id}")
    public CommResult<WfajInfo> find(@PathVariable String id) {
        WfajInfo result = wfajInfoService.selectByPrimaryKey(id);

        Map<String,Object> data = new HashMap<>();
        data.put("wfajInfo",result);
        return CommResult.success(data);

    }

    /**
     * 保存违法案件
     *
     * @param wfajInfo
     * @return
     */
    @ApiOperation("保存违法案件")
    @PostMapping("/save")
    public CommResult<WfajInfo> save(@RequestBody WfajInfo wfajInfo) {
        wfajInfo.setWfajbh(CodeGenerator.getUUID());
        wfajInfo.setCreateDate(new Date());
        wfajInfo.setModifyDate(new Date());
        wfajInfo.setModifyBy("");
        wfajInfo.setCreateBy("");
        int affectCount = wfajInfoService.insert(wfajInfo);
        if (affectCount <= 0) {
            return CommResult.error();
        }
        WfajInfo result = wfajInfoService.selectByPrimaryKey(wfajInfo.getWfajbh());
        return CommResult.success(result);

    }


    /**
     * 批量删除违法案件
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除违法案件", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除违法案件,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<WfajInfo> del(@PathVariable String ids) {

        wfajInfoService.deleteByIds(ids.split(","));
        return CommResult.success();
    }

    /**
     * 更新违法案件
     *
     * @param wfajInfo
     * @return
     */
    @Log(desc = "更新违法案件", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新违法案件")
    @PostMapping("/update")
    public CommResult<WfajInfo> update(@RequestBody WfajInfo wfajInfo) {
        wfajInfo.setCreateDate(new Date());
        wfajInfo.setModifyDate(new Date());
        wfajInfo.setModifyBy("");
        wfajInfo.setCreateBy("");
        int affectCount = wfajInfoService.updateByPrimaryKeySelective(wfajInfo);
        if (affectCount <= 0){
            return CommResult.error();
        }
        WfajInfo result = wfajInfoService.selectByPrimaryKey(wfajInfo.getWfajbh());
        return CommResult.success(result);
    }

}
